const load = (function () {
    let loding = () => {
        $(".header").load("./header.html", function () {})
        $(".footer").load("./footer.html", function () {})
        $(".cbl").load("./cbl.html", function () {})
    }

    return {
        loding
    }
})();
$(function () {
    load.loding();
});
const list = (function () {
    axios.defaults.baseURL = "";
    const loadData = async () => {
        let data = await axios({
            url: "/data/list.json"
        })
        let C1 = window.location.href.split("?")[1];
        let C2 = 1;
       
        if (C1 != undefined) {
            C2 = C1.split("=")[1];
        }
        let res = data.data;
        let strHtml = ``;
        let strhtml1 = ``;
        let strhtml2 = ``;
        res.forEach(el => {
            if (el.spid == C2) {
                strHtml += `
                <ul class="fl">`
                let imgs = el.imgs;
                imgs.forEach(ele => {
                    strHtml += `<li>
                    <div class="tdivp">
                        <img src="http://127.0.0.1:8080/${ele}" alt="">
                    </div>
                </li>`
                })
                strHtml += `
                    </ul>
                    <div class="dahz fr">
                        <img src="http://127.0.0.1:8080/${el.imgs[0]}" alt="">
                    </div>
                    <div class="bshare-custom left icon-medium">
                        <span style="line-height: 45px;color: #666666;font-size: 13px;float: left;">登录分享赢好礼：</span>
                        <a title="分享到微信" href=""> <img src="./imges/ia_100000099.gif" alt=""> </a>
                        <a title="分享到新浪微博" href=""><img src="./imges/ia_100000100.gif" alt=""></a>
                        <a title="分享到QQ空间" href=""><img src="./imges/ia_100000101.gif" alt=""></a>
                        <a class="aimg" href=""> <img class="imga" src="./imges/ia_1400000076.png"
                        alt=""><span>收藏商品</span></a>
                    </div>`
                strhtml1 = `
                    <h3 class="itemtitle">${el.name}</h3>
                    <P class="jsj">茅台风骨 酱香典范.</P>
                    <div class="hd">
                        <a href="https://www.gjw.com/trydrink/ShowPage-id-6683.htm" id="GotoTryDrink"
                        class="sellingBox">
                        <h3>特卖会</h3>
                        <span>此商品正在参加特卖会,&nbsp;<em id="djs-time">
                                <div class="colockbox" id="colockbox1" style="display: inline-block;">
                                    <em class="hour">00</em>小时&nbsp;
                                    <em class="minute">00</em>分&nbsp;
                                    <em class="second">00</em>秒&nbsp;
                                </div>
                            </em>&nbsp;后结束,请尽快购买!
                        </span>
                    </a>
                </div>
                <div class="priceBox">
                    <div class="ps"><span class="hdj">活动价</span><span class="rmb">￥</span><span class="jg">${el.price}</span></div>
                    <div class="hdh"><span class="cxhd">促销信息</span><span class="xsqg">限时抢购</span></div>
                </div>
                <div>
                    <div class="psd">
                        <span class="psz">配上至:</span>
                        <span class="adder">上海 ∨</span>
                        <span class="dbdd">单笔订单不满100元，收运费10元 <img src="./imges/ia_1400000078.png" alt=""> 满百包邮</span>
                    </div>
                </div>
                <div class="gls Universal">
                    <ul>
                        <li>关注度<span class="gzd">79207</span></li>
                        <li>累计评价<a class="pl" href="">40</a></li>
                        <li class="xb">送积分<span class="sjf">788</span></li>
                    </ul>
                </div>
                <div class="sljj Universal">
                    <span>数量:</span><input class="sl" type="text" value="1">
                    <span><a class="jia" href="javascript:void(0)">+</a><a class="jian" href="javascript:void(0)">-</a></span> 
                </div>
                <div class="gmwc">
                    <a class="ljgm" href="">立即购买</a>
                    <a class="jrgw" href=""><p class="ppp"></p>加入购物车</a>
                    <div ><p class="pp"></p> 手机下单更优惠
                        <img class="yimg" src="./imges/ia_1000000058.png" alt="">
                    </div>
                </div>`
                strhtml2 = ` <span><a target="_blank" href="./index.html">首页</a> </span>&gt;
                <span>${el.name}</span>`
            }
        });
        $(".category").html(strhtml2);
        $(".sp-l").html(strHtml);
        $(".sp-r").html(strhtml1);
        $(".sp-l").qh();
        $(".sp-l").fd();
        $(".sljj").dji();
        axios.interceptors.request.use(function (config) {
            config.headers.token = localStorage.getItem("token");
            return config;
        })
        let spid = C2;
        axios({
            url: "goods/goodsById?spid=" + spid
        }).then(function (res) {
            $(".jrgw").data("info", res.data)
        })


        $(function () {
            $(".jrgw").on("click", function () {
                //购买人的id
                let uId = JSON.parse(localStorage.getItem("user") || '{}').uid;
                //取当年存的值

                let spId = $(".jrgw").data("info").spid;
                let sname = $(".jrgw").data("info").name;
                let simg = $(".jrgw").data("info").img;
                let sprice = $(".jrgw").data("info").price;
                let ssum = $(".sl").val();
                let data = new URLSearchParams();
                data.append('spid', spId);
                data.append('sname', sname);
                data.append('simg', simg);
                data.append('ssum', ssum);
                data.append('sprice', sprice);
                axios({
                    url: "/cart/addCart",
                    method: "post",
                    data: data
                }).then(function (res) {
                    layer.msg(res.data.msg);
                })
            return false;
            })

        })
    }


    return {
        loadData
    }
})()
$(function () {
    list.loadData()
});
(function () {
    $.fn.extend({
        qh() {
            $(this).find("li").on("mouseenter", function () {
                $(this).find(".tdivp").css("border", "2px solid #cc0c17");
                $(this).siblings("li").find(".tdivp").css("border", "1px solid rgb(194, 192, 192)");
                $(this).find(".tdivp").css({
                    "width": 72,
                    "height": 72
                });
                $(this).find(".tdivp img").css({
                    "margin-top": 0
                });
                $(this).siblings("li").find(".tdivp").css({
                    "width": 74,
                    "height": 74
                })
                $(this).siblings("li").find(".tdivp img").css({
                    "margin-top": 1
                });
                $(this).parents("ul").parent().find(".dahz img").attr("src", $(this).find("img").attr("src"))
            })
        },
        fd() {
            $(this).find(".dahz img").on("mouseenter", function (e) {
                var ss = $(this).css({
                    "width": "650px",
                    "height": "650px"
                })
                $(ss).mousemove(function (e) {
                    var d = (ss.width() - $(".dahz").width()) / $(".dahz").width();
                    var ssc = e.pageX * d - $(".dahz").offset().left * d;
                    var ssc1 = e.pageY * d - $(".dahz").offset().top * d;
                    $(ss).css({
                        left: -ssc,
                        top: -ssc1
                    });
                }).on("mouseleave", function () {
                    $(ss).css({
                        "width": "420px",
                        "height": "420px",
                        "margin-top": "0px",
                        "margin-left": "0px",
                        "top": "0px",
                        "left": "0px"
                    })
                })
            })
        },
        dji() {
            var s = 0;
            $(this).find(".jia").on("click", function () {
                s = $(".sl").val();
                s++
                $(".sl").val(s)
            })
            $(this).find(".jian").on("click", function () {
                s = $(".sl").val();
                s--
                if (s <= 0) {
                    s = 1;
                }
                $(".sl").val(s)
            })
        }
    })
})()